<?php
$op = get_get("op");
global $_G;
$uid = $_G["logininfo"]["uid"];
if ($op == "post") {
    $actiontype = get_post("actiontype");
    if ($actiontype == "active") {
        $message = get_post("text");
        if (strlen($message) >= 2000) {
            fail("消息长度不能大于2000个字节");
        }
        if ($actiontype == "active") {
            sendActive($uid, $message);
            success("发送成功");
        }
        if ($actiontype == "leavemessage") {
            $target = get_post("target");
            if ($target == "") {
                fail("无效的参数,请重试");
            }
            sendLeaveMessage($uid, $target, $message);
            success("发送成功");
        }
    }
}
if ($op == "removeactive") {
    $aid = get_get("aid");
    ;
    if ($aid == "") {
        fail("无效的参数");
    }
    $data = fetchActiveByAid($aid);
    if (! $data) {
        fail("你要删除的动态不存在");
    }
    if ($_G["profile"]["uid"] != $data["userid"]) {
        // TODO 其他管理者权限判定
        fail("你没有权限这样做");
    }
    removeCommentByAid($aid);
    removeActive($aid);
    die(json_encode(array(
        "state" => 1,
        "message" => "删除成功",
        "id" => $aid
    )));
}
if ($op == "listActive") {
    if (get_get("uid") != "") {
        $uid = get_get("uid");
    }
    $page = get_get("page");
    if ($page == "") {
        $page = 1;
    }
    $result = fetchActiveByPage($uid, $page);
    if (! $result) {
        fail("这里空空如也.");
    }
    $backcode = array(
        "state" => 1,
        "maxpage" => getActiveMaxPage($uid)
    );
    foreach ($result as $value) {
        $backcode[$value["aid"]] = encodeActive($value);
    }
    die(json_encode($backcode));
}

function encodeActive($data)
{
    global $_G;
    $allowAdmin = false;
    $name = null;
    ;
    if ($_G["logininfo"]["uid"] == $data["userid"]) {
        $allowAdmin = true;
        $name = $_G["profile"]["name"];
    } else {
        $profile = getProfile($data["userid"]);

        if (! $profile) {
            global $_GCONFIG;
            return array(
                "aid" => $data["aid"],
                "has_comment" => false,
                "content" => "作者被禁止或不存在,相关内容被屏蔽",
                "userid" => - 1,
                "date" => $data["time"],
                "name" => - 1,
                "userImageURL" => $_GCONFIG["user"]["default"]["headimage"],
                "allowAdmin" => false
            );
        }
        $name = $profile["name"];
    }
    return array(
        "aid" => $data["aid"],
        "has_comment" => $data["has_comment"],
        "content" => $data["content"],
        "userid" => $data["userid"],
        "date" => $data["time"],
        "name" => $name,
        "userImageURL" => $_G["profile"]["image"],
        "allowAdmin" => $allowAdmin
    );
}
?>